//
//  SwitchTableViewCell.m
//  GiftSay
//
//  Created by QQn1993 on 16/1/15.
//  Copyright © 2016年 liyinkai. All rights reserved.
//

#import "SwitchTableViewCell.h"

@interface SwitchTableViewCell ()

@property (nonatomic,strong)UIImageView  *img;
@property (nonatomic,strong) UILabel *label;
@property (nonatomic,strong) UIView *view;
@property (nonatomic,strong) UIButton *btn;

@end

@implementation SwitchTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.img = [[UIImageView alloc] init];
        self.img.contentMode = UIViewContentModeScaleAspectFill;
        [self.contentView addSubview:self.img];
        
        self.label = [[UILabel alloc] init];
        [self.contentView addSubview:self.label];
        
        self.view = [[UIView alloc] init];
        
//        self.view.backgroundColor = [UIColor greenColor];
        self.view.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.view.layer.borderWidth = 2;
        [self.contentView addSubview:self.view];
        
        self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.btn.backgroundColor = [UIColor whiteColor];
        self.btn.layer.masksToBounds = YES;
        self.btn.layer.borderWidth = 2;
        self.btn.layer.borderColor = [UIColor lightGrayColor].CGColor;

        [self.btn addTarget:self action:@selector(touch) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.btn];
    }
    return self;
}

- (void)touch
{
    self.btn.selected = !self.btn.selected;
    if (self.btn.selected == YES) {
        [UIView animateWithDuration:0.5 animations:^{
            self.btn.frame = CGRectMake(self.width - self.height / 3 * 5 , self.height / 6, self.height / 3 * 2, self.height / 3 * 2);
            
            self.view.backgroundColor = [UIColor whiteColor];
        }];
        
        if ([self.title isEqualToString:@"夜间模式"]) {
            //数据持久化方法记录夜间模式
            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"NightMode"];
            //通知 tabbar改变色
            [[NSNotificationCenter defaultCenter] postNotificationName:@"NightModeOff" object:nil];
            self.backgroundColor = [UIColor whiteColor];
            self.label.textColor = [UIColor blackColor];
            
        }
        else if([self.title isEqualToString:@"接收消息"]){
            NSLog(@"接收推送关闭");
        }
        
    }
    else{
        [UIView animateWithDuration:0.5 animations:^{
            self.btn.frame = CGRectMake(self.width - self.height / 3 * 5 + self.height / 3 * 2 , self.height / 6, self.height / 3 * 2, self.height / 3 * 2);
            self.view.backgroundColor = [UIColor greenColor];
            
        }];
        
        if ([self.title isEqualToString:@"夜间模式"]) {
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"NightMode"];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"NightModeOpen" object:nil];
            self.backgroundColor = kNightModeColor;
            self.label.textColor = [UIColor whiteColor];
        }
        else if([self.title isEqualToString:@"接收消息"]){
            NSLog(@"接收推送开启");
        }
    }
    
    [self.delegate sendSwich:self.btn.selected AndTitle:self.title];
    
    
}


- (void)setTitle:(NSString *)title
{
    _title = title;
    self.label.text = [NSString stringWithFormat:@"   %@",title];
    if ([title isEqualToString:@"夜间模式"]) {
        self.btn.selected = ![[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"];
    }
    else if([title isEqualToString:@"接收消息"]){
        self.btn.selected = NO;
    }
}

- (void)setImgname:(NSString *)imgname
{
    _imgname = imgname;
    self.img.image = [UIImage imageNamed:imgname];
}

- (void)layoutSubviews
{
    [super subviews];
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES){
        self.backgroundColor = kNightModeColor;
        self.label.textColor = [UIColor whiteColor];
    }
    else{
        self.backgroundColor = [UIColor whiteColor];
        self.label.textColor = [UIColor blackColor];
    }
    
    self.img.frame = CGRectMake(10, 4, self.height - 8, self.height - 8);
    self.view.frame = CGRectMake(self.width - self.height / 3 * 5, self.height / 6, self.height / 3 * 4, self.height / 3 * 2);
    self.view.layer.masksToBounds = YES;
    self.view.layer.cornerRadius = (self.height ) / 3;
    
    if ([self.title isEqualToString:@"夜间模式"] && self.btn.selected == YES) {
        self.btn.frame = CGRectMake(self.width - self.height / 3 * 5 , self.height / 6, self.height / 3 * 2, self.height / 3 * 2);
        self.view.backgroundColor = [UIColor whiteColor];
    }
    else{
        self.btn.frame = CGRectMake(self.width - self.height / 3 * 5 + self.height / 3 * 2 , self.height / 6, self.height / 3 * 2, self.height / 3 * 2);
        self.view.backgroundColor = [UIColor greenColor];
    }
    self.btn.layer.cornerRadius = (self.height ) / 3;
    
    self.label.frame = CGRectMake(self.img.frame.size.width + 10, self.height / 3, self.width - self.view.frame.size.width - self.img.width - 10, self.height / 3);
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
